package com.micai.springboot.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.micai.springboot.base.BaseController;
import com.micai.springboot.dto.MenuDto;
import com.micai.springboot.service.MenuService;
import com.micai.springboot.service.RoleMenuService;
import com.micai.springboot.shiro.ShiroService;
import com.micai.springboot.util.MessageUtils;
import com.micai.springboot.vo.MenuVo;
import com.micai.springboot.vo.tree.TreeMenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：菜单管理
 * <p>
 * Author: 赵新国
 * Date: 2017/11/14 11:35
 */
@Controller
@RequestMapping("/admin/menu")
public class AdminMenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private ShiroService shiroService;

    /**
     * 分页查询菜单列表
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="10") int pageSize,
                       @RequestParam(required = false) String searchParam, Model model) {
        Page<MenuDto> menuDtos = menuService.getMenuByPage(pageNum, pageSize, searchParam);
        if (menuDtos == null && menuDtos.getResult().isEmpty()) {
            return null;
        }
        PageInfo<MenuDto> page = new PageInfo<>(menuDtos);
        model.addAttribute("page",page);
        // 参数回显
        model.addAttribute("searchParam", searchParam);
        return "admin/menu/menuList";
    }

    /**
     * 获取菜单树结构
     * @param name
     * @param model
     * @return
     */
    @GetMapping(value = "/tree")
    @ResponseBody
    public Map<String, Object> tree(@RequestParam(required = false) String name, Model model) {
        Map<String, Object> retMap = null;
        try {
            List<TreeMenuVo> menuTree = menuService.getMenuByTree(name, null, null);
            if (menuTree != null) {
                retMap = MessageUtils.success();
                retMap.put("menuTree", menuTree);
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 保存菜单信息
     *
     * @param menuVo
     * @return
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(@ModelAttribute MenuVo menuVo) {
        Map<String, Object> retMap = MessageUtils.success();
        try {
            MenuDto menuDto = new MenuDto();
            BeanUtils.copyProperties(menuVo, menuDto);
            menuDto.setCreator(getId());
            menuDto.setModifier(getId());
            if (menuService.save(menuDto) == 1) {
                retMap = MessageUtils.success("保存成功");
                // 更新权限
//                shiroService.updatePermission();
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据ID查询菜单信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Map<String, Object> get(@PathVariable("id") Long id) {
        Map<String, Object> retMap = MessageUtils.success();
        try {
            MenuDto menuDto = menuService.getMenuById(id);
            if (menuDto != null) {
                // 根据上级ID查询对应的菜单信息
                MenuDto parentMenuDto = menuService.getMenuByParentId(menuDto.getpId());
                if (parentMenuDto != null) {
                    menuDto.setpName(parentMenuDto.getName());
                }
                retMap.put("menuDto", menuDto);
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 修改菜单信息
     *
     * @param menuVo
     * @return
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public Map<String, Object> edit(@ModelAttribute MenuVo menuVo) {
        Map<String, Object> retMap = null;
        try {
            MenuDto menuDto = menuService.getMenuById(menuVo.getId());
            if (menuDto != null) {
                BeanUtils.copyProperties(menuVo, menuDto);
                menuDto.setModifier(getId());
                // 修改菜单
                if (menuService.update(menuDto) == 1) {
                    retMap = MessageUtils.success("修改成功");
                    // 更新权限
//                    shiroService.updatePermission();
                    return retMap;
                }
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据ID批量删除菜单信息
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Map<String, Object> deleteBatch(@RequestParam("ids[]") List<Long> ids) {
        Map<String, Object> retMap = null;
        try {
            for (Long id : ids) {
                // 删除菜单
                if (menuService.deleteById(id)) {
                    retMap = MessageUtils.success("删除成功");
                    // 更新权限
//                    shiroService.updatePermission();
                    return retMap;
                }
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 获取某一个角色对应菜单
     * @param id
     * @return
     */
    @GetMapping(value = "/getMenus/{id}")
    @ResponseBody
    public Map<String, Object> getMenusByRoleId(@PathVariable("id") Long id) {
        Map<String, Object> retMap = null;
        try {
            List<MenuDto> menuDtos = menuService.getMenusByRoleId(id);
            retMap = MessageUtils.success();
            retMap.put("menuDtos", menuDtos);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 获取当前菜单的上级
     * @param id
     * @return
     */
    @GetMapping(value = "/getParentMenuById/{id}")
    @ResponseBody
    public Map<String, Object> getParentMenuById(@PathVariable("id") Long id) {
        Map<String, Object> retMap = null;
        try {
            MenuDto menuDto = menuService.getParentMenusById(id);
            retMap = MessageUtils.success();
            retMap.put("menuDto", menuDto);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }
}
